#include "sm59d04g2sfr.h"
#include "DEFINE.h"

//===============================

//==============================

#define ISP_IAP_BYTE_PROGRAM 0x80   //
#define ISP_IAP_SECTOR_ERASE 0x82   //

uchar isp_read_byte(uint add)
{

    unsigned char code *tempP;
 		unsigned char rdata;
        tempP = add+0x3000;
 		rdata = *tempP; 
        return rdata;
 
}
//=========================================================================
// ISP Enable
//=========================================================================

void EEPROM_Enable(void)
{
	ISPFD = 0x55;			// Enable ISP function key
	ISPFD = 0xAA;
	ISPFD = 0x55;
	SCONF |= 0x06;		// Set ISP & 768 Byte Enable SFR
	EA	=	0;					//add by raleigh 20071025
}
//=========================================================================
// ISP Disable
//=========================================================================
void EEPROM_Disable(void)
{
	SCONF &= 0xFB;		// Set ISP Disable SFR
	EA	=	1;					//add by raleigh 20071025
}

void isp_program_byte(uint add, uchar dat)
{
	uchar addh, addl;
	EEPROM_Enable();
	ISPFD = dat;
        add=add+0x3000;  //
	addh = add >> 8;
	addl = add & 0xff;
	ISPFAH = addh;
	ISPFAL = addl;

	ISPC = ISP_IAP_BYTE_PROGRAM;
	ISPC = ISP_IAP_BYTE_PROGRAM;

	ACC = 0;
	EEPROM_Disable();

}
void isp_erase_sector(uchar  num)
{

	uchar addh, addl;
        num=0;
	EEPROM_Enable();        

	addh = 0x30;	//
	addl = 0x00;
	ISPFAH = addh;
	ISPFAL = addl;

	ISPC = ISP_IAP_SECTOR_ERASE;
	ISPC = ISP_IAP_SECTOR_ERASE;

	ACC = 0;
	EEPROM_Disable();	

}

















